<html>
<head><meta charset="utf-8"><title>Parentheses in patterns · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Parentheses.20in.20patterns.html">Parentheses in patterns</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="235995139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Parentheses%20in%20patterns/near/235995139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Parentheses.20in.20patterns.html#235995139">(Apr 24 2021 at 18:50)</a>:</h4>
<p>I was surprised to find out today that this doesn't parse:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="mi">0</span><span class="o">..=</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="w">    </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>(Filed &lt;<a href="https://github.com/rust-lang/rust/issues/84528">https://github.com/rust-lang/rust/issues/84528</a>&gt; about the error message.)</p>
<p>What's the current state of parens in patterns?  Is this something that we could just consider allowing in the grammar?</p>
<p>Spotted in &lt;<a href="https://github.com/rust-lang/rust/pull/83918/files#diff-b2588fe10cfa40f26a678938f0efe8984afee13b8ddc81c458a4d65d54de9a2dR4">https://github.com/rust-lang/rust/pull/83918/files#diff-b2588fe10cfa40f26a678938f0efe8984afee13b8ddc81c458a4d65d54de9a2dR4</a>&gt;, in the form <code>0..=(1+2)</code>, where the parens are more useful, albeit not allowed.</p>



<a name="235998591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Parentheses%20in%20patterns/near/235998591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Parentheses.20in.20patterns.html#235998591">(Apr 24 2021 at 19:32)</a>:</h4>
<p>LHS and RHS in range patterns are just very limited and expect either literals, or paths, or <code>const</code> blocks, but not arbitrary patterns (including parentheses).</p>



<a name="235998759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Parentheses%20in%20patterns/near/235998759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Parentheses.20in.20patterns.html#235998759">(Apr 24 2021 at 19:34)</a>:</h4>
<p>Regarding <code>1 + 2</code> in the parentheses, parentheses in patterns still expect other patterns inside the parentheses rather than arbitrary expressions, for arbitrary expressions we have <code>const { ... }</code> blocks.</p>



<a name="235998844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Parentheses%20in%20patterns/near/235998844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Parentheses.20in.20patterns.html#235998844">(Apr 24 2021 at 19:35)</a>:</h4>
<p>(Nothing prevents suggesting const blocks in error messages for cases like <code>0 ..= (1 + 2)</code> though.)</p>



<a name="236004390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Parentheses%20in%20patterns/near/236004390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Parentheses.20in.20patterns.html#236004390">(Apr 24 2021 at 20:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123856">Vadim Petrochenkov</span> <a href="#narrow/stream/213817-t-lang/topic/Parentheses.20in.20patterns/near/235998591">said</a>:</p>
<blockquote>
<p>LHS and RHS in range patterns are just very limited and expect either literals, or paths, or <code>const</code> blocks, but not arbitrary patterns (including parentheses).</p>
</blockquote>
<p>Ah, I that makes sense as it's not like <code>0..=(3 | 4)</code> with a subpattern there would make any sense.  Thanks.</p>
<p>(Agreed about  <code>const{}</code> being the way for <code>(1+2)</code>; I don't want to propose a change to make that a legal pattern on its own.)</p>



<a name="236033787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Parentheses%20in%20patterns/near/236033787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Parentheses.20in.20patterns.html#236033787">(Apr 25 2021 at 03:39)</a>:</h4>
<p>Yeah, I mentioned this in <a href="https://github.com/rust-lang/rust/issues/67264#issuecomment-812770692">https://github.com/rust-lang/rust/issues/67264#issuecomment-812770692</a>, where I tried to find as many edges as I could. ^^</p>



<a name="236033883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Parentheses%20in%20patterns/near/236033883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Parentheses.20in.20patterns.html#236033883">(Apr 25 2021 at 03:41)</a>:</h4>
<p>and as I mentioned in the PR,</p>
<blockquote>
<p>e.g. There is, at least, a mild diagnostic improvement in some edge cases, because before this PR the pattern<code> 0..=(5+1)</code> encounters the <code>half_open_range_patterns</code> feature gate and can thus emit the request to enable the feature flag, while also emitting the "inclusive range with no end" diagnostic. There is no intent to allow an <code>X..=</code> pattern that I am aware of, so so removing the flag request is a strict improvement. The arrival of the <code>J | K</code> "or" pattern also enables some odd formations.</p>
</blockquote>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>